/**
 * Created by Administrator on 2018/5/27 0027.
 */
//easyui  验证框重写

//删除chanpin
function shanchu() {
    var row = $('#dg').datagrid('getSelected');
    if (row) {
        $.messager.confirm('Confirm', '是否确认删除' + row.gname + '?', function (r) {
            if (r) {
                $.ajax({
                    url: path + '/gongyingshang/del.do',
                    dataType: 'json',
                    type: 'POST',
                    data: {
                        id: row.id
                    },
                    success: function (data) {
                        if (data.code == 200) {
                            $.messager.show({
                                title: '提示',
                                msg: "删除" + row.gname + '成功!',
                                timeout: 3000,
                                showType: 'slide'
                            });
                            $("#dg").datagrid({
                                url: path + '/gongyingshang/list.do'
                            });
                        } else {
                            $.messager.alert('提示', data.msg);
                        }
                    }
                })
            }
        });
    } else {
        $.messager.alert('提示', '请选择要操作的数据');
    }


}
//导出excel
function downExcel() {
    window.location.href = path + "/gongyingshang/downloadexcel.xls?leixing=" + $("#ss").searchbox("getName") + "&gname=" + $("#ss").searchbox("getValue");
}
//打开窗口
function openWindow(code) {
    clear();
    if (code == 2) {
        $('#addwindow').dialog({title: "修改供应商"});

        var row = $('#dg').datagrid('getSelected');
        if (row) {
            $.ajax({
                url: path + '/gongyingshang/find/id.do',
                dataType: 'json',
                type: 'POST',
                data: {
                    id: row.id
                },
                success: function (data) {
                    if (data.code == 200) {
                        $("#id").val(data.rows.id)
                        $("#gname").textbox("setValue", data.rows.gname);
                        $("#phone").textbox("setValue", data.rows.phone);
                        $("#person").textbox("setValue", data.rows.person);
                        $("#leixing").combobox("setValue", data.rows.leixing);
                        $("#address").textbox("setValue", data.rows.address);
                        $("#addwindow").window('open');
                    } else {
                        $.messager.alert('提示', data.msg);
                    }
                }
            })
        } else {
            $.messager.alert('提示', '请选择要操作的数据');
            return;
        }
    } else {
        $("#id").val("")
        $('#addwindow').dialog({title: "新增供应商"});
        $("#addwindow").window('open');
    }

}
//保存供应商信息
function save() {

    var gname = $("#gname").val().trim();
    if (gname == "" || gname == null) {
        $("#gnametest").text('*供应商名称不能为空');
        return;
    } else {
        $("#gnametest").text('');
    }
    var leixing = $("#leixing").combobox("getValue");
    //解决第一次取值错误的问题
    if (leixing == '自产') {
        leixing = 0
    }
    $.ajax({
        url: path + '/gongyingshang/save.do',
        dataType: 'json',
        type: 'POST',
        data: {
            id: $("#id").val(),
            gname: gname,
            phone: $("#phone").val(),
            person: $("#person").val().trim(),
            leixing: leixing,
            address: $("#address").val()
        },
        success: function (data) {
            if (data.code == 200) {
                var sts = "";
                if ($("#id").val().trim() != "" && $("#id").val().trim() != null) {
                    sts = '修改';
                } else {
                    sts = '添加';
                }
                $.messager.show({
                    title: '提示',
                    msg: sts + gname + '成功!',
                    timeout: 3000,
                    showType: 'slide'
                });
                $("#dg").datagrid({
                    url: path + '/gongyingshang/list.do'
                });
                $("#addwindow").window('close');
            } else {
                $.messager.alert('警告', data.msg);
            }
        }
    })
}
//清除所有值
function clear() {
    $("#id").val("")
    $("#gname").textbox("setValue", "");
    $("#gnametest").text('');
    $("#leixing").combobox("setValue", "自产");
    $("#person").textbox("setValue", "");
    $("#phone").textbox("setValue", "")
    $("#address").textbox("setValue", "")
}
